package gui;

import cards.Card;
import cards.Card.Suit;
import cards.Card.Face;
import cards.CardDeck;
import game.PokerPlayer;

import javax.swing.JFrame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TexasHoldem extends game.TexasHoldem {
  private JFrame main;
  private Table table;

  public TexasHoldem(int players, JFrame main) {
    super(players);

    this.table = new Table();
    this.main = main;

    this.main.setSize(table.getSize());
    this.main.getContentPane().add(table);
    this.main.setVisible(true);
  }

  private void sleep(int msec) {
    try {
      Thread.sleep(msec);
    } catch(InterruptedException e) {}
  }

  @Override
  protected void addPlayer(int playerNum) {
    super.addPlayer(playerNum);
    this.table.addPlayer(playerNum);
  }

  @Override
  protected void dealBeginning() {
    super.dealBeginning();
    this.table.resetCards();
  }

  @Override
  protected void holeCardDealt(PokerPlayer p, Card c) {
    super.holeCardDealt(p, c);
    table.setPlayerCard(p.getId(), p.getAllCards().size() - 1, c);
    sleep(300);
  }

  @Override
  protected void holeCardsDealt() {
    super.holeCardsDealt();
    sleep(1200);
  }

  @Override
  protected void flopDealt() {
    super.flopDealt();
    for (int i = 0; i < FLOP_CARDS; i++) {
       table.addCommunityCard(comm.get(i));
    }
    sleep(1200);
  }

  @Override
  protected void turnDealt() {
    super.turnDealt();
    table.addCommunityCard(comm.get(3));
    sleep(1200);
  }

  @Override
  protected void riverDealt() {
    super.riverDealt();
    table.addCommunityCard(comm.get(4));
    sleep(1200);
  }

  @Override
  protected void revealWinner(PokerPlayer p) {
    super.revealWinner(p);
    // TODO: display fancy stuff
  }

  public static void main(String[] args) {
     TexasHoldem g = null;
     int players = 2;
     int rounds = 1;

     if (args.length >= 1) {
        try {
           players = Integer.parseInt(args[0]);
        } catch (NumberFormatException e) {
           System.err.println("Invalid number of players. Please enter 2 - 10.");
           System.exit(1);
        }
     }

     if (args.length >= 2) {
        try {
           rounds = Integer.parseInt(args[1]);
        } catch (NumberFormatException e) {
           System.err.println("Invalid number of rounds. Must be an integer.");
           System.exit(1);
        }
     }
     
     JFrame main = new JFrame("Texas Holdem");
     main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     g = new TexasHoldem(players, main);
     g.initGame();
     for (int i = 0; i < rounds; i++) {
       System.out.println("Starting round [" + i + "]");
       g.runGame();
       if (i+1 < rounds) {
         g.sleep(5000);
       }
     }
  }
}
